﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Security.Claims;
using AppSocketModel;
using AppSocketService.Account;
using BCGPSCommon;

namespace AppSocketService
{
    public  class ClaimsAccessor : IClaimsAccessor
    {
        protected IPrincipalAccessor principalAccessor { get; } 

        public ClaimsAccessor(IPrincipalAccessor principalAccessor )
        {
            this.principalAccessor = principalAccessor; 
        }

        public string AccountId
        {
            get
            {
                var code = principalAccessor.principal?.Claims.FirstOrDefault(s => s.Type == ClaimTypes.Name)?.Value;
                if (code.Contains("AccountId:"))
                    return code.Replace("AccountId:", ""); 
                else
                    throw new Exception("登录信息异常,请重新登录!");
            }
        }

        public AccountRole AccountRole
        {
            get
            {
                string AccountRoleStr = principalAccessor.principal?.Claims.FirstOrDefault(s => s.Type == ClaimTypes.Role)?.Value;
                if (string.IsNullOrWhiteSpace(AccountRoleStr))
                    throw new Exception("登录账号异常,请重新登录!");
                return (AccountRole)Enum.Parse(typeof(AccountRole), AccountRoleStr, true);
            }
        } 
    }
}
